﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SB;

namespace 养老金支付系统
{
    public partial class FormYCXZFLR : FormBase
    {
        public FormYCXZFLR()
        {
            InitializeComponent();
        }

        string delzfbh; 
        string delxm;
        int zflx;
        string grbh = string.Empty;
        string xm;
        string sfzh;
        int yhbh;
        string yhhm;
        string yhzh;
        decimal zhfh;
        decimal szf;
        decimal fxj;
        decimal tbf;
        decimal zfhj;
        string bz;
        const string ycxzfsql = @"select 支付编号,支付类型,个人编号,姓名,账户返还,丧葬费,抚恤金,退补发,
            支付金额,银行名称,银行户名,银行帐号,备注,支付录入时间 as 录入时间 from 查支付明细 
            where 支付期号=dbo.GetNextQH(dbo.GetMaxZZQH()) and 支付类型码>0 order by 支付录入时间 desc";
        
        void init()
        {
            this.Text = "一次性支付信息录入";
            //this.grbh = "";
            this.delzfbh = "";
            this.delxm = "";
            this.zflx = 2;
            this.xm = "";
            this.sfzh = "";
            this.yhbh = 0;
            this.yhhm = "";
            this.yhzh = "";
            this.zhfh = 0;
            this.szf = 0;
            this.fxj = 0;
            this.tbf = 0;
            this.zfhj = 0;
            this.bz = "";
            textBox1.Text = SqlHelper.getScalar("select dbo.GetNextQH(dbo.GetMaxZZQH())") as string;
            //textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "0";
            textBox5.Text = "0";
            textBox6.Text = "0";
            textBox7.Text = "0";
            textBox8.Text = "0";
            textBox9.Text = "";
            textBox10.Text = "";
            textBox11.Text = "";
            comboBox1.DisplayMember = "显示";
            comboBox1.DataSource = SqlHelper.getDataTable("select cast(支付类型码 as char(2))+' '+支付类型 as 显示 from 支付类型");
            comboBox1.SelectedIndex = 2;
            comboBox2.DisplayMember = "显示";
            comboBox2.DataSource = SqlHelper.getDataTable("select cast(银行编号 as char(2))+' '+银行名称 as 显示 from 支付银行");
            dataGridView1.DataSource = SqlHelper.getDataTable(ycxzfsql);
            if (this.dataGridView1.Rows.Count <= 0)
            {
                this.delzfbh = "";
                this.delxm = "";
                button2.Enabled = false;
            };
        }

        void find()
        {
            try
            {
                if (PPS.Cookies != string.Empty)
                {
                    if (this.grbh.Length == 10)
                    {
                        QueryOncePay qop = new QueryOncePay(PPS.Cookies);
                        if (qop.find(this.grbh))
                        {
                            textBox2.Text = qop.GRBH;
                            textBox3.Text = qop.XM;
                            this.sfzh = qop.SFZH;
                            comboBox1.SelectedIndex = qop.ZFLX;
                            textBox4.Text = qop.ZHFH.ToString();
                            textBox5.Text = qop.SZF.ToString();
                            textBox6.Text = qop.FXJ.ToString();
                            textBox7.Text = "0";
                            textBox8.Text = qop.YFHJ.ToString();
                            comboBox2.SelectedIndex = qop.YHBH;
                            textBox9.Text = qop.YHHM;
                            textBox10.Text = qop.YHZH;
                            textBox11.Text = qop.BZ;
                        }
                        else
                        {
                            init();
                        }
                    }
                    else
                    {
                        init();
                    }
                }
            }
            catch {
                init();
            }
        }

        void add()
        {
            if (this.grbh.Length == 10
                && this.xm !=""
                && this.zflx >= 1
                && this.zfhj > 0
                && this.yhhm.Length > 0
                && this.yhzh.Length > 0)
            {
                SqlParameter[] p = new SqlParameter[] { 
                    new SqlParameter("@ZFLX",this.zflx),
                    new SqlParameter("@GRBH",this.grbh),
                    new SqlParameter("@XM",this.xm),
                    new SqlParameter("@SFZH",this.sfzh),
                    new SqlParameter("@ZHFH",this.zhfh),
                    new SqlParameter("@SZF",this.szf),
                    new SqlParameter("@FXJ",this.fxj),
                    new SqlParameter("@TBF",this.tbf),
                    new SqlParameter("@ZFHJ",this.zfhj),
                    new SqlParameter("@YHBH",this.yhbh),
                    new SqlParameter("@YHHM",this.yhhm),
                    new SqlParameter("@YHZH",this.yhzh),
                    new SqlParameter("@ZFBZ",this.bz)
                };
                if (SqlHelper.exeProcReturn("P_一次性支付录入", p) == 1)
                    MessageBox.Show(this, "添加成功！", "添加", MessageBoxButtons.OK, MessageBoxIcon.Information);
                else
                    MessageBox.Show(this, "添加失败！", "添加", MessageBoxButtons.OK, MessageBoxIcon.Error);
                dataGridView1.DataSource = SqlHelper.getDataTable(ycxzfsql);
            }
            else
            {
                MessageBox.Show(this, "录入的数据有误！", "添加", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        void del()
        {
            if (this.delzfbh !="")
            {
                string msg = "确定删除支付编号[" + this.delzfbh + "]姓名[" + this.delxm + "]的一次性支付信息？";
                if (MessageBox.Show(this, msg, "删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK)
                {
                    if (SqlHelper.exeProcReturn("P_一次性支付删除", new SqlParameter("@ZFBH", this.delzfbh)) >= 0)
                    {
                        MessageBox.Show(this, "删除成功！", "删除", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        init();
                    }
                    else
                        MessageBox.Show(this, "删除失败！", "删除", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void FormYCXZFLR_Load(object sender, EventArgs e)
        {

            Login l = new Login();
            if (l.IsLogin)
            {
                PPS.Cookies = l.Cookies;
                this.Text = l.UserName;
            }
            else
            {
                PPS.Cookies = string.Empty;
            }
            init();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.zflx = comboBox1.SelectedIndex;
            switch (this.zflx)
            {
                case 2:
                textBox5.ReadOnly = false;
                textBox6.ReadOnly = false;
                textBox7.ReadOnly = true;
                textBox7.Text = "0";
                    break;
                case 1:
                    textBox5.ReadOnly = false;
                    textBox6.ReadOnly = false;
                    textBox7.ReadOnly = false;
                    break;
                case 0:
                    comboBox1.SelectedIndex = 2;
                    this.zflx = comboBox1.SelectedIndex;
                    break;
                default:
                    textBox5.ReadOnly = true;
                    textBox6.ReadOnly = true;
                    textBox7.ReadOnly = true;
                    textBox5.Text = "0";
                    textBox6.Text = "0";
                    textBox7.Text = "0";
                    break;
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.yhbh = comboBox2.SelectedIndex;
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            TextBox t = sender as TextBox;
            try
            {
                decimal d = decimal.Parse(t.Text.ToString());
                if (t.Text.Remove(0, t.Text.Length - 1) != ".")
                    t.Text = d.ToString();
                t.SelectionStart = t.Text.Length;
                this.zhfh = decimal.Parse(this.textBox4.Text);
                this.szf = decimal.Parse(this.textBox5.Text);
                this.fxj = decimal.Parse(this.textBox6.Text);
                this.tbf = decimal.Parse(this.textBox7.Text);
                this.zfhj = this.szf + this.fxj + this.zhfh + this.tbf;
                this.textBox8.Text = this.zfhj.ToString();

            }
            catch (Exception)
            {
                t.Text = "0";
                t.SelectAll();
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            this.grbh = textBox2.Text;
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            this.xm = textBox3.Text;
        }

        private void textBox9_TextChanged(object sender, EventArgs e)
        {
            this.yhhm = textBox9.Text;
        }

        private void textBox10_TextChanged(object sender, EventArgs e)
        {
            this.yhzh = textBox10.Text;
        }

        private void textBox11_TextChanged(object sender, EventArgs e)
        {
            this.bz = textBox11.Text;
        }

        private void textBox2_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                find();
        }

        private void textBox2_Leave(object sender, EventArgs e)
        {
            find();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            add();
        }

        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridView1.Rows.Count > 0)
            {
                this.delzfbh = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
                this.delxm = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
                button2.Enabled = true;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            del();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DataTable dt = dataGridView1.DataSource as DataTable;
            if (dt != null)
            {
                SaveFileDialog sfd = new SaveFileDialog() { Title = "保存一次性支付明细导出文件", 
                    FileName = textBox1.Text + "期一次性支付明细", Filter = "Excel(97-2003)|*.xls" };
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    ExcelHelper.DataTableToExcel(dt, sfd.FileName);
                }
            }
        }

        private void textBox10_Leave(object sender, EventArgs e)
        {
            if (!PPS.IsCardNumberValid(this.yhzh))
                MessageBox.Show(this, "银行帐号有误！", "帐号检查", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
    }
}
